const OSS = require('ali-oss')
const path = require('path')
const { v4: uuidv4 } = require('uuid')

// OSS客户端配置
const client = new OSS({
  region: process.env.OSS_REGION || 'oss-cn-hangzhou',
  accessKeyId: process.env.OSS_ACCESS_KEY_ID || 'your-access-key-id',
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET || 'your-access-key-secret',
  bucket: process.env.OSS_BUCKET || 'yundayin-files',
  secure: true
})

// 支持的文件类型
const SUPPORTED_FILE_TYPES = {
  // 文档类型
  'application/pdf': {
    name: 'PDF文档',
    extension: '.pdf',
    maxSize: 50 * 1024 * 1024, // 50MB
    category: 'document'
  },
  'application/msword': {
    name: 'Word文档',
    extension: '.doc',
    maxSize: 50 * 1024 * 1024,
    category: 'document'
  },
  'application/vnd.openxmlformats-officedocument.wordprocessingml.document': {
    name: 'Word文档',
    extension: '.docx',
    maxSize: 50 * 1024 * 1024,
    category: 'document'
  },
  'application/vnd.ms-excel': {
    name: 'Excel表格',
    extension: '.xls',
    maxSize: 50 * 1024 * 1024,
    category: 'document'
  },
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': {
    name: 'Excel表格',
    extension: '.xlsx',
    maxSize: 50 * 1024 * 1024,
    category: 'document'
  },
  'application/vnd.ms-powerpoint': {
    name: 'PowerPoint演示文稿',
    extension: '.ppt',
    maxSize: 50 * 1024 * 1024,
    category: 'document'
  },
  'application/vnd.openxmlformats-officedocument.presentationml.presentation': {
    name: 'PowerPoint演示文稿',
    extension: '.pptx',
    maxSize: 50 * 1024 * 1024,
    category: 'document'
  },
  'text/plain': {
    name: '文本文档',
    extension: '.txt',
    maxSize: 10 * 1024 * 1024, // 10MB
    category: 'document'
  },
  
  // 图片类型
  'image/jpeg': {
    name: 'JPEG图片',
    extension: '.jpg',
    maxSize: 20 * 1024 * 1024, // 20MB
    category: 'image'
  },
  'image/png': {
    name: 'PNG图片',
    extension: '.png',
    maxSize: 20 * 1024 * 1024,
    category: 'image'
  },
  'image/gif': {
    name: 'GIF图片',
    extension: '.gif',
    maxSize: 20 * 1024 * 1024,
    category: 'image'
  },
  'image/bmp': {
    name: 'BMP图片',
    extension: '.bmp',
    maxSize: 20 * 1024 * 1024,
    category: 'image'
  },
  'image/webp': {
    name: 'WebP图片',
    extension: '.webp',
    maxSize: 20 * 1024 * 1024,
    category: 'image'
  },
  'image/tiff': {
    name: 'TIFF图片',
    extension: '.tiff',
    maxSize: 20 * 1024 * 1024,
    category: 'image'
  }
}

// 最大文件大小 (50MB)
const MAX_FILE_SIZE = 50 * 1024 * 1024

/**
 * 生成唯一文件名
 * @param {string} originalName - 原始文件名
 * @returns {string} 生成的文件名
 */
function generateFileName(originalName) {
  const ext = path.extname(originalName)
  const timestamp = Date.now()
  const uuid = uuidv4().replace(/-/g, '').substring(0, 8)
  return `${timestamp}_${uuid}${ext}`
}

/**
 * 获取文件存储路径
 * @param {number} userId - 用户ID
 * @param {string} fileName - 文件名
 * @returns {string} 文件存储路径
 */
function getFilePath(userId, fileName) {
  const date = new Date()
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  
  return `files/${userId}/${year}${month}${day}/${fileName}`
}

/**
 * 获取预览图存储路径
 * @param {number} userId - 用户ID
 * @param {string} fileName - 文件名
 * @returns {string} 预览图存储路径
 */
function getPreviewPath(userId, fileName) {
  const date = new Date()
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  
  return `previews/${userId}/${year}${month}${day}/${fileName}`
}

module.exports = {
  client,
  SUPPORTED_FILE_TYPES,
  MAX_FILE_SIZE,
  generateFileName,
  getFilePath,
  getPreviewPath
}